<?
//Class Auto Numbering
class autonumber {
        function autonumber($tglbkt,$delimeted="",$counterLength=5) {
                global $DBConnection;
                $SQL2 = "SELECT right(ktrasl,1) FROM tblprofile";
                            $KTRASL = $DBConnection->dbc->get_var($SQL2);
                if (strlen($delimeted)) $this->delimeted=$KTRASL.$delimeted;
                else $this->delimeted=$KTRASL;

                list($this->DD,$this->MM,$this->YY) = explode("/",$tglbkt);
                if ($this->DD<10) $this->DD = "0".($this->DD*1);
                if ($this->MM<10) $this->MM = "0".($this->MM*1);
//                print $this->DD." ".$this->MM." ".$this->YY;die();
                $this->YY = substr($this->YY, -2);
                $this->counterLength = $counterLength;
        }

        function GetNumber($Prefix) {
                global $DBConnection;
                $NumberStr = $Prefix.$this->YY;
                switch ($Prefix) {
                        case "TK":
                        case "TB":
                        case "BK":
                        case "BB":
/*
                                $SQL = "SELECT nobkt FROM ais_memorial WHERE nobkt like '$NumberStr%' AND SUBSTRING(nobkt,7,2)='{$this->delimeted}' ORDER BY RIGHT(nobkt,5) DESC LIMIT 1";
*/
//                                $SQL = "SELECT nobkt FROM ais_memorial WHERE nobkt like '$NumberStr%' ORDER BY RIGHT(nobkt,5) DESC LIMIT 1";
                               $SQL = "SELECT nobkt FROM ais_memorial WHERE nobkt like '$NumberStr%' AND SUBSTRING(nobkt,7,".(strlen($this->delimeted)).")='{$this->delimeted}' ORDER BY RIGHT(nobkt,8) DESC LIMIT 1";
                                break;
                        case "ZTK":
                        case "ZTB":
                        case "ZBK":
                        case "ZBB":
                                $SQL = "SELECT nobkt FROM ais_memorial_z WHERE nobkt like '$NumberStr%' AND SUBSTRING(nobkt,8,".(strlen($this->delimeted)).")='{$this->delimeted}' ORDER BY RIGHT(nobkt,8) DESC LIMIT 1";
                                break;
                        case "BS":
                                $SQL = "SELECT nobkt FROM ais_advance WHERE nobkt like '$NumberStr%' ORDER BY RIGHT(nobkt,6) DESC LIMIT 1";
                                break;
                        case "KW":
                                $SQL = "SELECT nokwit FROM ais_receipt WHERE nokwit like '$NumberStr%' ORDER BY RIGHT(nokwit,6) DESC LIMIT 1";
                                break;
                        //Rama, (17092009), Start
                        case "MP":
                                $SQL = "SELECT nomp FROM ais_gainloss_status WHERE nomp like '$NumberStr%' ORDER BY RIGHT(nomp,6) DESC LIMIT 1";
                                break;
                        case "MB":
                                $SQL = "SELECT nobkt FROM ais_memorial WHERE nobkt like '$NumberStr%' ORDER BY RIGHT(nobkt,6) DESC LIMIT 1";
                                break;
                        //Rama, (17092009), End
                        default:
                                $SQL = "SELECT nobkt FROM ais_memorial WHERE nobkt like '$NumberStr%' ORDER BY RIGHT(nobkt,6) DESC LIMIT 1";
                                //echo "$SQL"; exit;

                }
    //die($SQL);
                $NumberStr = $NumberStr.$this->MM;
//                          $NumberStr  = $NumberStr.$KTRASL.$this->delimeted;
                            $NumberStr  = $NumberStr.$this->delimeted;
                $LastTransNo = $DBConnection->dbc->get_var($SQL);
                //die($LastTransNo);
                $Urut = substr($LastTransNo, -$this->counterLength);
                $Urut = intval($Urut)+1;
                $SQL = "SELECT LPAD('$Urut',".$this->counterLength.",'0')";
                $Urut = $DBConnection->dbc->get_var($SQL);
                $NumberStr = $NumberStr.$Urut;
                return $NumberStr;
        }
}
?>
